/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client.activity;

import java.io.IOException;

import nu.zoom.android.gdata.project.client.UIUtilities;
import nu.zoom.android.gdata.project.client.WKC;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient.Feed;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ArrayAdapter;

/**
 * @author Johan Maasing
 * 
 */
public abstract class FeedrunnerListActivity<FeedType extends Feed<RowType>, RowType> extends ListActivity {
	private final FeedRunner feedRunner;
	protected ArrayAdapter<RowType> adapter;
	protected int startPosition = 1;
	protected int numEntries;
	protected Uri feedUri;
	private final int layoutID;

	/**
	 * 
	 * @param layoutID
	 *            The view layout.
	 * @param rowlayoutID
	 *            The row layout.
	 */
	public FeedrunnerListActivity(final int layoutID) {
		super();
		this.layoutID = layoutID;
		feedRunner = new FeedRunner(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(layoutID);
		adapter = getAdapter() ;
		setListAdapter(adapter);
		
		final Intent intent = getIntent();
		feedUri = intent.getData();
	}

	protected void runFeedQuery() {
		final SharedPreferences sharedPreferences = getSharedPreferences(WKC.PREFERENCES_NAME, MODE_PRIVATE);
		FeedParam parameter = new FeedParam();

		parameter.startIndex = startPosition;
		parameter.cannedQuery = sharedPreferences.getString(WKC.PREFERENCES.CANNED_KEY, null);
		parameter.batchSize =
				new Integer(sharedPreferences.getString(WKC.PREFERENCES.BATCH_KEY, WKC.BATCH_NUMBER_OF_ISSUES));

		feedRunner.run(feedUri, parameter);
	}
	
	protected abstract ArrayAdapter<RowType> getAdapter();

	class FeedRunner extends UIUtilities.FeedRunner<FeedType, FeedParam> {
		public FeedRunner(Activity activity) {
			super(activity);
		}

		@Override
		protected void result(FeedType result) {
			issueResult(result);
		}

		@Override
		protected FeedType runInBackground(Uri uri, FeedParam parameter) throws IOException {
			return FeedrunnerListActivity.this.runInBackground(uri, parameter);
		}
	}

	protected abstract FeedType runInBackground(Uri uri, FeedParam parameter) throws IOException;

	static class FeedParam {
		Integer startIndex;
		Integer batchSize;
		String cannedQuery;
	}

	protected void issueResult(final FeedType issueFeed) {
		adapter.clear();
		if (issueFeed.getList() != null) {
			for (RowType row : issueFeed.getList()) {
				adapter.add(row);
			}
		}
		numEntries = issueFeed.totalResult;
	}
}
